package com.snake.vargoappstore.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snake.vargoappstore.R;


public class CustomBar extends RelativeLayout {

    private TextView mText1;
    private TextView mText2;
    private TextView mTitle;
    private RadioGroup mRadioGroup;
    private LinearLayout mSearchTitleView;

    public enum BarMode {
        HOME,CLASS,RANKING,SEARCH,MANAGER
    }

    public CustomBar(Context context) {
        super(context);
        initView(context);
    }

    public CustomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.custom_bar, this);
        initSearchTitleView(context);
        initRadioG(context);
        mText1 = (TextView) findViewById(R.id.bar_tv_text1);
        mTitle = (TextView) findViewById(R.id.bar_tv_title);
        mText2 = (TextView) findViewById(R.id.bar_tv_text2);
    }

    private void initSearchTitleView(Context context) {
        mSearchTitleView = (LinearLayout) View.inflate(context, R.layout.search_title, null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(mSearchTitleView,params);
        mSearchTitleView.setVisibility(GONE);
    }

    private void initRadioG(Context context) {
        mRadioGroup = (RadioGroup) View.inflate(context, R.layout.manager_tab, null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(mRadioGroup,params);
        mRadioGroup.setVisibility(GONE);
    }

    public void setMode(BarMode barMode){
        mTitle.setText("应用商店");
        setVisibility(VISIBLE);
        int count = getChildCount();
        switch (barMode){
            case HOME:
            case CLASS:
                updateHome(count);
                break;
            case RANKING:
                updateHome(count);
                mTitle.setText("分类");
                break;
            case SEARCH:
                updateSearch(count);
                break;
            case MANAGER:
                updateManager(count);
                break;

        }

    }

    private void updateManager(int count) {
        for(int j = 0;j<count;j++){
            if(j == count-1){
                getChildAt(j).setVisibility(View.VISIBLE);
            }else {
                getChildAt(j).setVisibility(View.GONE);
            }
        }
    }

    private void updateSearch(int count) {
        for(int j = 0;j<count;j++){
            if(j == count-2){
                getChildAt(j).setVisibility(View.VISIBLE);
            }else {
                getChildAt(j).setVisibility(View.GONE);
            }
        }
    }

    private void updateHome(int count) {
        for(int j = 0;j<count;j++){
            if(j == count-1||j==count-2){
                getChildAt(j).setVisibility(View.GONE);
            }else {
                getChildAt(j).setVisibility(View.VISIBLE);
            }
        }
    }

    public void updateCustomBar(int i){
        switch (i){
            case 0:
                this.setMode(CustomBar.BarMode.HOME);
                break;
            case 1:
                this.setMode(CustomBar.BarMode.CLASS);
                break;
            case 2:
                this.setMode(CustomBar.BarMode.RANKING);
                break;
            case 3:
                this.setMode(CustomBar.BarMode.SEARCH);
                break;
            case 4:
                this.setMode(CustomBar.BarMode.MANAGER);
                break;
        }
    }

}
